/**
 * @author wxn
 * @since 12-5-24 1:24
 */
package com.sihai.ehr.web.action;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.Message;
import com.sihai.ehr.model.sys.User;
import com.sihai.ehr.service.MessageService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.AuthorityManager;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import javax.servlet.http.HttpSession;

@Results({@Result(name = "index", location = "index.jsp"),
        @Result(name = "error", location = "error.jsp")})
public class IndexAction extends EHRAction {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 7204414972672240877L;

    @BeanInject
    private MessageService messageService;

    private int messageCount;

    @Action("/index")
    @Override
    public String execute() throws Exception {
        //TODO:do action
        HttpSession session = ServletActionContext.getRequest().getSession();
        Object tempId = session == null ? null : session.getAttribute(AuthorityManager.user_id_session);
        Long userId = null;
        if(tempId != null){
            if(StringUtils.equals(AuthorityManager.super_admin_str_id, tempId.toString())){
                return "index";
            }
            userId = (Long)tempId;
        }
        //User user = getCurrentUser();
        if(userId != null && userId > 0){
            LimitedList<Message> messages = messageService.findByLoginUser(userId, null, null);
            messageCount = (messages == null ? 0 : messages.size());
        }
        return "index";
    }

    public int getMessageCount() {
        return messageCount;
    }
}
